package com.football;

import data.FootBallClubConstants;
import data.FootBallMemberConstants;
import data.SalaryBase;
import model.FootBallMember;
import model.MostFooterEnum;
import model.Position;

import java.awt.event.*;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Formatter;
import java.util.List;

import javax.swing.*;

public class AddMemberFrame extends JDialog {
	// 提示信息
    private JLabel jLabel1;

    private JLabel referNameLable;

    // 姓名label
    private JLabel nameLable;

    // 年龄
    private JLabel ageLabel;

    // 年龄域
    private JTextField ageTextField;

    // 确定添加按钮
    private JButton okBtn;

    // 返回列表按钮
    private JButton backBtn;

    // 球队列表
    private JLabel clubLable;

    // 球队下拉列表框
    private JComboBox clubBox;

    // 能力值lable
    private JLabel abilityLable;

    // 能力值域
    private JTextField abilityFiled;

    // 国家队lable
    private JLabel isNationLable;

    // 国家队下拉列表
    private JComboBox isNationBox;

    // 身高lablel
    private JTextField heightField;

    // 身高域
    private JLabel hegihtLable;

    // 薪水域
    private JTextField salaryFiled;

    // 薪水lable
    private JLabel salaryLable;

    // 惯用脚lable
    private JLabel mostFooterLable;

    // 惯用脚下拉列表
    private JComboBox mostFooterBox;

    // 场上位置lable
    private JLabel postionLabel;
    // 场上位置列表

    private JComboBox positionBox;

    // 姓名文本域
    private JTextField nameTextFiled;


    // 定义分隔符
    private JSeparator jSeparator1;

    public AddMemberFrame() {
        initGUI();
    }

    private void initGUI() {
        try {
            getContentPane().setLayout(null);
            this.setTitle("Add the information of football players !");

            jLabel1 = new JLabel();
            getContentPane().add(jLabel1);
            // 新增球员提示
            jLabel1.setText("Please, enter the whole information !");
            jLabel1.setBounds(88, 12, 243, 17);
            jLabel1.setForeground(new java.awt.Color(255, 0, 0));

            referNameLable = new JLabel();
            getContentPane().add(referNameLable);
            referNameLable.setText("Name");
            referNameLable.setBounds(33, 41, 66, 17);

            jSeparator1 = new JSeparator();
            getContentPane().add(jSeparator1);
            jSeparator1.setBounds(-6, 74, 390, 10);

            // 球员姓名
            nameLable = new JLabel();
            getContentPane().add(nameLable);
            nameLable.setText("Name");
            nameLable.setBounds(33, 84, 55, 17);

            // 姓名域
            nameTextFiled = new JTextField();
            getContentPane().add(nameTextFiled);
            nameTextFiled.setBounds(106, 83, 216, 24);

            // 年龄
            ageLabel = new JLabel();
            getContentPane().add(ageLabel);
            ageLabel.setText("Age");
            ageLabel.setBounds(33, 117, 55, 17);

            // 年龄域
            ageTextField = new JTextField();
            getContentPane().add(ageTextField);
			//光标离开后，计算薪水值
			ageTextField.addFocusListener(new FocusAdapter() {
				@Override
				public void focusLost(FocusEvent e) {
					//当年龄计算完毕，需要自动计算出薪水
					int nowAge = Integer.parseInt(ageTextField.getText());
					//当前薪水
					BigDecimal salary = new BigDecimal(nowAge- SalaryBase.BASE_AGE).
							multiply(new BigDecimal(SalaryBase.BASE_STEP)).
							multiply(SalaryBase.START_SALARY);
					DecimalFormat format = new DecimalFormat("0.00");

					//根据年龄计算所得值
					salaryFiled.setText(format.format(salary));
				}
			});
            ageTextField.setBounds(106, 113, 216, 24);

            // 惯用脚
            mostFooterLable = new JLabel();
            getContentPane().add(mostFooterLable);
            mostFooterLable.setText("Most footer");
            mostFooterLable.setBounds(33, 153, 55, 17);

            // 惯用脚的下拉列表
            mostFooterBox = new JComboBox();
            // 添加管用脚的下拉列表值
            for (MostFooterEnum mostFooterEnum : MostFooterEnum.values()) {

                mostFooterBox.addItem(mostFooterEnum.toString());

            }
            getContentPane().add(mostFooterBox);
            mostFooterBox.setBounds(106, 150, 216, 24);

            // 场上位置
            postionLabel = new JLabel();
            getContentPane().add(postionLabel);
            postionLabel.setText("Position");
            postionLabel.setBounds(33, 191, 55, 17);

            // 场上位置下拉列表

            positionBox = new JComboBox();
            getContentPane().add(positionBox);
            // 添加场上位置的下拉列表之
            for (Position position : Position.values()) {

                positionBox.addItem(position.toString());

            }
            positionBox.setBounds(106, 184, 216, 24);

            // 薪水
            salaryLable = new JLabel();
            getContentPane().add(salaryLable);
            salaryLable.setText("Salary");
            salaryLable.setBounds(33, 227, 46, 17);

            // 薪水文本域
            salaryFiled = new JTextField();
            getContentPane().add(salaryFiled);
            // 不可编辑，是根据年龄计算的值
            salaryFiled.setEditable(false);
            salaryFiled.setBounds(106, 224, 67, 24);

            // 身高
            hegihtLable = new JLabel();
            getContentPane().add(hegihtLable);
            hegihtLable.setText("Height");
            hegihtLable.setBounds(202, 226, 38, 17);

            // 身高文本域
            heightField = new JTextField();
            getContentPane().add(heightField);
            heightField.setBounds(252, 223, 70, 24);

            // 所属球队
            clubLable = new JLabel();
            getContentPane().add(clubLable);
            clubLable.setText("Club");
            clubLable.setBounds(33, 266, 59, 17);

            // 球队文本域
            clubBox = new JComboBox();
            // 添加球队的下拉列表值
            List<String> clubs = FootBallClubConstants.getALlFootBallData();
            for (String club : clubs) {

                clubBox.addItem(club);

            }
            getContentPane().add(clubBox);
            clubBox.setBounds(104, 265, 211, 24);

            // 能力值
            abilityLable = new JLabel();
            getContentPane().add(abilityLable);
            abilityLable.setText("Ability");
            abilityLable.setBounds(33, 305, 59, 17);

            // 能力值文本域
            abilityFiled = new JTextField();
            getContentPane().add(abilityFiled);
            abilityFiled.setBounds(104, 300, 211, 24);

            // 是否国家队成员
            isNationLable = new JLabel();
            getContentPane().add(isNationLable);
            isNationLable.setText("Nation Team");
            isNationLable.setBounds(33, 352, 59, 17);

            // 国家队文本域
            isNationBox = new JComboBox();
            // 国家队成员列表
            isNationBox.addItem("YES");
            isNationBox.addItem("NO");
            getContentPane().add(isNationBox);
            isNationBox.setBounds(104, 352, 211, 24);

            okBtn = new JButton();
            getContentPane().add(okBtn);
            okBtn.setText("Confirm");
            okBtn.setBounds(92, 400, 79, 24);
            okBtn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    okBtnActionPerformed(evt);
                }
            });

            backBtn = new JButton();
            getContentPane().add(backBtn);
            backBtn.setText("Back");
            backBtn.setBounds(219, 400, 84, 24);
            backBtn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    backBtnActionPerformed();
                }
            });

            this.setSize(400, 600);
            // super.center();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 点击确认按钮，执行添加逻辑
     * 
     * @param evt
     */
    private void okBtnActionPerformed(ActionEvent evt) {
        // 姓名
        String name = nameTextFiled.getText();
        // 年龄
        int age = Integer.parseInt(ageTextField.getText());
        // 惯用脚
        String mostFooterString = mostFooterBox.getSelectedItem().toString();
        MostFooterEnum mostFooterEnum = null;
        for (MostFooterEnum mostFooterEnum1 : MostFooterEnum.values()) {

            if (mostFooterEnum1.toString().equals(mostFooterString)) {
                mostFooterEnum = mostFooterEnum1;
            }

        }
        if (mostFooterEnum == null) {
            // 默认右脚
            mostFooterEnum = MostFooterEnum.RIGHT_FOOTER;
        }
        // 场上位置
        String positionString = positionBox.getSelectedItem().toString();
        Position position = null;
        for (Position position1 : Position.values()) {

            if (position1.toString().equals(positionString)) {
                position = position1;
            }

        }
        if (position == null) {
            // 默认位置
            position = Position.GUARD;

        }
        // 年薪
        BigDecimal salary = new BigDecimal(salaryFiled.getText());
        // 所在球队
        String club = clubBox.getSelectedItem().toString();
        // 球员身高
        String height = heightField.getText();
        // 能力值
        int ability = Integer.parseInt(abilityFiled.getText());
        // 国家队成员
        boolean isNation = false;
        String isNationString = isNationBox.getSelectedItem().toString();
        if (isNationString.equals("YES")) {
            isNation = true;
        }

        if (name == null || name.equals("")) {
            JOptionPane.showMessageDialog(this, "Must enter the name !");
            nameTextFiled.setFocusable(true);
			return;
        }
		List<FootBallMember> members = FootBallMemberConstants.getFootBallMembers();
		for (FootBallMember member : members){
			if (member.getName().equals(name)){
				//存在当前用户名
				JOptionPane.showMessageDialog(this,"The player has existed,please enter again!");
				nameTextFiled.setFocusable(true);
				return;
			}

		}
		if (age < 16){
			JOptionPane.showMessageDialog(this,"The age can not be in collegiate career，please enter again!");
			ageTextField.setFocusable(true);
			return;
		}else if (age > 35){
			JOptionPane.showMessageDialog(this,"The age has been out collegiate career，please enter again!");
			ageTextField.setFocusable(true);
			return;
		}
		if (position == Position.GOALKEEPER){
			//如果是门将，必须满足身高
			if (Integer.parseInt(height) < 185){
				JOptionPane.showMessageDialog(this,"The goalkeepers' height must be beyond 185cm!");
				heightField.setFocusable(true);
				return;
			}
		}
		if (isNation){
			//如果是国家队，需要满足能力值90以上
			if (ability < 90){
				JOptionPane.showMessageDialog(this,"The players' ability must be beyond 90,so that can enter nation team!");
				ageTextField.setFocusable(true);
				return;
			}

		}

        // 当前修改的member
        FootBallMember currentMember = new FootBallMember(age, mostFooterEnum, position, salary, club, name, height,
                ability, isNation);


        if (JOptionPane.showConfirmDialog(this, "Confirm to add the player?", "Reminder",
                JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            // 添加到列表
            members.add(currentMember);
            // 更新数据
            FootBallMemberConstants.setFootBallMembers(members);
        }

    }

    /**
     * 点击返回按钮
     */
    protected void backBtnActionPerformed() {
        // 关闭当前frame
        this.setVisible(false);
        this.dispose();
        // 显示列表frame
        DispMemberFrame dispMemberFrame = new DispMemberFrame();
        dispMemberFrame.setVisible(true);

    }

    public static Object getGUIBuilderInstance() {
        return new UpdateMemberFrame(Boolean.FALSE);
    }

    public AddMemberFrame(Boolean initGUI) {
        super();
    }

}